//
//  ContainerExpenseIncomeController.m
//  MoneyIndividual
//
//  Created by Le Anh Tai on 1/1/14.
//  Copyright (c) 2014 Le Anh Tai. All rights reserved.
//

#import "ContainerExpenseIncomeView.h"
#import "ExpenseIncomeController.h"
#import "RootViewController.h"
#import "MenuMediator.h"
#import "iSwipeGesture.h"

@implementation ContainerExpenseIncomeView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        [self setupAnchorImageView];
        
        [self setupExpenseIncomeController];
        
        _swipeGesture = [[iSwipeGesture alloc] initSwipeLRWithAnchorLeftCenter:(SCREEN_SIZE.width - SWIPE_WIDTH)/2 anchorRightCenter:SCREEN_SIZE.width*1.5 - SWIPE_WIDTH/2 - DELTA_ANCHOR];
        _swipeGesture.swipeDelegate = self;
        [self addGestureRecognizer:_swipeGesture];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(slidingViewDidAnchor:) name:ECSlidingViewTopDidReset object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(slidingViewDidAnchor:) name:ECSlidingViewTopDidAnchorRight object:nil];
    }
    
    return self;
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)setupAnchorImageView
{
    _anchorImageView = [[UIImageView alloc] init];
    UIImage *image = [UIImage imageNamed:@"icon_anchor.png"];
    [_anchorImageView setImage:image];
    _anchorImageView.size = image.size;
    [self addSubview:_anchorImageView];
    CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);
    _anchorImageView.transform = transform;
    _anchorImageView.alpha = 0.5;
}

- (void)setupExpenseIncomeController
{
    _expenseIncomeController = [[ExpenseIncomeController alloc] init];
    [[RootViewController shared] addChildViewController:_expenseIncomeController];
    
    _expenseIncomeController.view.layer.masksToBounds = NO;
    _expenseIncomeController.view.layer.shadowOffset = CGSizeMake(-5, 0);
    _expenseIncomeController.view.layer.shadowRadius = 7;
    _expenseIncomeController.view.layer.shadowOpacity = 0.3;
    
    [self addSubview:_expenseIncomeController.view];
}

- (void)keyboardWillShow:(NSNotification *)aNotification
{
    _isKeyboardShow = YES;
}

- (void)keyboardWillHide:(NSNotification *)aNotification
{
    _isKeyboardShow = NO;
}

- (void)slidingViewDidAnchor:(NSNotification *)aNotification
{
    self.hidden = NO;
    
    if ([aNotification.name isEqualToString:ECSlidingViewTopDidAnchorRight])
    {
        self.hidden = YES;
    }
    
}

- (void)animationToLeft
{
    [_swipeGesture animationToLeft];
}

- (void)animationToRight
{
    [_swipeGesture animationToRight];
}

- (void)selectExpenseTab
{
    [_expenseIncomeController selectExpenseTab];
}

- (void)selectIncomeTab
{
    [_expenseIncomeController selectIncomeTab];
}

- (void)setFrame:(CGRect)frame
{
    [super setFrame:frame];
    
    _expenseIncomeController.view.frame = RECT(SWIPE_WIDTH, 0, SCREEN_SIZE.width, self.height);
    
    _anchorImageView.right = _expenseIncomeController.view.left + 5;
    _anchorImageView.centerY = self.height/2;
}

- (BOOL)pointInside:(CGPoint)point withEvent:(UIEvent *)event
{
    if (_isKeyboardShow && point.x <= SWIPE_WIDTH)
    {
        return NO;
    }
    
    if (point.x >=0)
    {
        int navigationBottom = HEI_HEADER_VIEW + HEI_STATUS_BAR;
        if ((point.y < navigationBottom && _swipeGesture.isAnchorLeft == NO) || [[MenuMediator shared].slidingController openMenu])
        {
            return NO;
        }
        
        return YES;
    }
    
    return NO;
}

#pragma mark - SwipeGesture delegate
- (void)iSwipeGestureDidAnchorRight:(iSwipeGesture *)gesture
{
    [self endEditing:YES];
    
    _expenseIncomeController.incomeController.managedObject = [[CoreData shared] newEntity:TRANSACTION_ENTITY];
}

@end
